package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.core.utils.CollUtil;
import com.dyna.core.utils.PageUtil;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.domain.resp.ResultCode;
import com.dyna.enums.CommonStatusEnum;
import com.dyna.foundation.domain.entity.SysTenant;
import com.dyna.foundation.domain.form.SysTenantSaveForm;
import com.dyna.foundation.domain.req.SysTenantPageReq;
import com.dyna.foundation.domain.resp.SysTenantResp;
import com.dyna.service.SysTenantService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

/**
 * @description 基础模块/租户模块
 * @author zhoucaiwnag
 * @date 2023-08-13
 */

@Api(value = "租户模块")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/tenant")
public class SysTenantController {

    private SysTenantService tenantService;

    @GetMapping("/get-id-by-name")
    @PermitAll
    @ApiOperation(value = "使用租户名，获得租户编号", notes = "登录界面，根据用户的租户名，获得租户编号")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "name", value = "租户名", required = true, example = "dyna")})
    public R<Long> getTenantIdByName(@RequestParam("name") String name) {
        SysTenant tenant = tenantService.getTenantByName(name);
        return R.data(tenant != null ? tenant.getId() : null);
    }

    @GetMapping("/get-by-website")
    @ApiOperation(value = "使用域名，获得租户信息", notes = "登录界面，根据用户的域名，获得租户信息")
    public R<SysTenantResp> getTenantByWebsite(@RequestParam("website") @ApiParam("域名") String website) {
        SysTenant tenant = tenantService.getTenantByWebsite(website);
        if (tenant == null || CommonStatusEnum.isDisable(tenant.getStatus())) {
            return R.success(ResultCode.SUCCESS);
        }
        return R.data(new SysTenantResp(tenant.getId(), tenant.getName()));
    }

    @GetMapping({ "simple-list" })
    @ApiOperation(value = "获取租户精简信息列表", notes = "只包含被开启的租户，用于【首页】功能的选择租户选项")
    public R<List<SysTenantResp>> getTenantSimpleList() {
        List<SysTenant> list = tenantService.getTenantListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return R.data(CollUtil.convertList(list, it -> BeanUtil.copyProperties(it, SysTenantResp.class)));
    }

    @GetMapping("/page")
    @ApiOperation(value = "获得租户分页")
    public R<PageResult<SysTenantResp>> getTenantPage(@Valid SysTenantPageReq pageVO) {
        PageResult<SysTenant> pageResult = tenantService.getTenantPage(pageVO);
        PageResult<SysTenantResp> result = PageUtil.convertPageResult(pageResult, SysTenantResp.class);
        return R.data(result);
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建租户")
    public R<Long> createTenant(@Valid @RequestBody SysTenantSaveForm createReqVO) {
        return R.data(tenantService.createTenant(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新租户")
    public R<Boolean> updateTenant(@Valid @RequestBody SysTenantSaveForm updateReqVO) {
        tenantService.updateTenant(updateReqVO);
        return R.isSuccess(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除租户")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<Boolean> deleteTenant(@RequestParam("id") Long id) {
        tenantService.deleteTenant(id);
        return R.isSuccess(true);
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得租户")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<SysTenantResp> getTenant(@RequestParam("id") Long id) {
        SysTenant tenant = tenantService.getTenant(id);
        return R.data(BeanUtil.toBean(tenant, SysTenantResp.class));
    }

}